<?php

include_once F_SOURCE . 'keywords/keywords.model.class.php';

/**
 * 广告投放管理CTRL类
 * 
 * @author Dean
 *
 */
class keywordsCtrl extends Ctrl{
	
	/**
	 * 显示投放列表
	 */
	public function show() {
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		
		$keywordsModel = new keywordsModel();
		
		$number = $keywordsModel->countAll();
		
		$keywordsList = $keywordsModel->findAll(($pageIndex-1)*PageNum, PageNum);	
		
		$page = pageCount($number, $pageIndex, '?do=keywords.show'); 
		
		$data = array(
			'op_select' => array('keywords'=>"class=select"),
			'keywordsList' => $keywordsList,
			'page' => $page
		);
		
		$view->display($data, 'keywords_show.html');
	}
	
	/**
	 * 查询单个投放信息
	 */
	public function info(){
		
		$view = new View();
		
		$id = $_GET['id'];
		
		$keywordsModel = new keywordsModel();
		$keywordsInfo = $keywordsModel->findOne($id);	
		
		$data = array(
			'op' => 'update',
			'op_select' => array('keywords'=>"class=select"),
			'obj' => $keywordsInfo
		);
		
		$view->display($data, 'keywords_view.html');
	}
	
	/**
	 * 跳转新增页面
	 */
	public function view(){
		$view = new View();
		
		$data = array(
			'op' => $_GET['op'],
			'op_select' => array('keywords'=>"class=select")
		);
		
		$view->display($data, 'keywords_view.html');
	}
	
	/**
	 * 新增投放操作
	 * 
	 */
	public function add(){
		
		$keywordsname =  getPostStringParam('name');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$keywords = getPostStringParam('keywords');
		
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
		
		$data = array(
			'name' => $keywordsname,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'keywords' => $keywords,
			'createTime' => $time,
			'modifyTime' => $time,
			'createUser' => $createUser
		);

		$keywordsModel = new keywordsModel();
		$insertID = $keywordsModel->add($data);
			
		if($insertID > 0){
			$this->showMessage('?do=keywords.show', 'op_success');
		}else{
			$this->showMessage('?do=keywords.view&op=add', 'op_failure');
		}
	}
	
	/**
	 * 修改投放信息操作，通过投放编号
	 * 
	 */
	public function modify(){
		
		$id = $_POST['id'];
		$keywordsname =  getPostStringParam('name');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$keywords = getPostStringParam('keywords');
		
		$time = getNowTime();
		
		$data = array(
			'name' => $keywordsname,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'keywords' => $keywords,
			'modifyTime' => $time
		);
		
		$where = array(
			'id' => $id
		);
		
		$keywordsModel = new keywordsModel();
		$number = $keywordsModel->modify($data, $where);
			
		if($number > 0){
			$this->showMessage('?do=keywords.show', 'op_success');
		}else{
			$this->showMessage('?do=keywords.view&op=add', 'op_failure');
		}
		
	}
	
	/**
	 * 删除用户操作，通过用户编号
	 * 
	 */
	public function delete(){
		
		$id = intval($_GET['id'])>0?intval($_GET['id']):0;
		
		if($id<1){
			$this->showMessage('?do=keywords.show', 'op_wrong_id');
		}
		
		$data = array(
			'id' => $id
		);
		
		$keywordsModel = new keywordsModel();
		$success = $keywordsModel->delete($data);
		
		if($success > 0){
			$this->showMessage('?do=keywords.show', 'op_success');
		}else{
			$this->showMessage('?do=keywords.show', 'op_failure');
		}
	}

}


?>